package dr.incomeapp.model;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import dr.incomeapp.bean.IncomeBean;
import dr.incomeapp.bean.IncomeBean.TIMEEVENT;
import dr.incomeapp.controller.Util;
import dr.incomeapp.dao.FactoryDao;
import dr.incomeapp.dao.IncomeDao;

public class IncomeManagerModel extends MyModel {

	IncomeDao incomeDao = FactoryDao.getIncomeDao();
	List<IncomeBean> listIncome = new LinkedList<IncomeBean>();
	Date startDate = new Date();
	

	public IncomeManagerModel() {
	
		refresh();
	}
	
	public void refresh(){
		//listIncome = incomeDao.readAll();
		
		Calendar cal = Calendar.getInstance();
	    cal.setTime(new Date());
	    int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	    cal.set(cal.get(Calendar.YEAR),Calendar.MONTH , days);
	    
	    /*
	    IncomeBean incomeBean = new IncomeBean();
	    incomeBean.setName("Saldo");
	    incomeBean.setStartDate(cal.getTime());
	    incomeBean.setMoney(getMoney());
	    incomeBean.setMoneyProg(getMoneyProg());  
	    incomeBean.setCost(incomeBean.getMoney() < 0);

	    listIncome.add(incomeBean);
	    */
	}
	
	public  List<IncomeBean> getListIncome(){
		return listIncome;
	}

	public void clear() {
		listIncome = null;
		
	}

	public void readAll() {
		//listIncome = incomeDao.readAll();
		
	}
	
	
	public float getMoney(){
		float money = 0;
		
		for(IncomeBean bean: listIncome){
			money += bean.getMoney();
		}
		
		
		return money;
	}
	
	public float getMoneyProg(){
		float money = 0;
		
		for(IncomeBean bean: listIncome){
			money += bean.getMoneyProg();
		}
		
		
		return money;
	}

	public Date getDate() {
		return startDate;
	}

	public void setDate(Date date) {
		this.startDate = date;
	}
	
	public void calcIncome(){
		Date endDate = new Date(startDate.getTime());
		
		Calendar cal = Calendar.getInstance();
	    cal.setTime(endDate);
	    int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) , maxDays);
		
		endDate = cal.getTime();
	    

		listIncome = new LinkedList<IncomeBean>();
		
		for(IncomeBean iB: incomeDao.readDateInterval(startDate, endDate)){
			
			TIMEEVENT timeEvent = iB.getTimeEvent();
			
			int tEvent = 0;
			
			
			switch(timeEvent){
				case ONCE: listIncome.add(iB);	break;
				
				case DAY:	tEvent = Calendar.DAY_OF_YEAR;	break;	
				case WEEK:	tEvent = Calendar.WEEK_OF_YEAR;	break;	
				case MONTH: tEvent = Calendar.MONTH;		break;
				case YEAR: 	tEvent = Calendar.YEAR;			break;
				
			}
		
			
			if(timeEvent != TIMEEVENT.ONCE){
				
				Date newDate = startDate;
				

						
				while(newDate.getTime() <= endDate.getTime()){
			
					iB.setStartDate(newDate);
					listIncome.add(iB);
			
					newDate = Util.addTime(newDate, tEvent);
				}
			}
			
		}
	}

	public void calcNextMonthIncome() {
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(startDate);
		cal.add(Calendar.MONTH, 1);
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) , 1);
		setDate(cal.getTime());
		
		calcIncome();
		
	}
	
	public void calcPrevMonthIncome() {
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(startDate);
		cal.add(Calendar.MONTH, -1);
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH) , 1);
		setDate(cal.getTime());
		
		calcIncome();
		
	}
	
	
	

}
